package me.firstapp.bts;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.beans.factory.annotation.Autowired;

import me.firstapp.bts.service.ApiClientJobService;
import me.firstapp.bts.service.OrderJobService;
import me.firstapp.bts.service.SearchJobService;
import me.firstapp.bts.service.SectionJobService;
import me.firstapp.bts.service.StatisticsJobService;
import me.firstapp.bts.service.TopicJobService;

public class JobTask {

	@Autowired
	private TopicJobService topicJobService;

	@Autowired
	private SectionJobService sectionJobService;

	@Autowired
	private OrderJobService orderJobService;

	@Autowired
	private SearchJobService searchJobService;

	@Autowired
	private StatisticsJobService statisticsJobService;

	@Autowired
	private ApiClientJobService apiClientJobService;

	private ExecutorService topicExecutorService = Executors.newFixedThreadPool(1);
	private ExecutorService sectionExecutorService = Executors.newFixedThreadPool(1);
	private ExecutorService cancelOrderExecutorService = Executors.newFixedThreadPool(1);
	private ExecutorService updateSearchDataExecutorService = Executors.newFixedThreadPool(1);
	private ExecutorService updateApiClientExecutorService = Executors.newFixedThreadPool(1);
	private ExecutorService statisticsExecutorService = Executors.newFixedThreadPool(1);

	/*
	 * 话题任务
	 */
	public synchronized void doTopicTask() {
		topicExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				topicJobService.updateTopics();
			}
		});
	}

	/*
	 * 板块任务
	 */
	public synchronized void doSectionTask() {
		sectionExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				sectionJobService.updateTopicAndReplyCounts();
			}
		});
	}

	/*
	 * 取消超时的订单
	 */
	public synchronized void doCancelOutTimeOrderTask() {
		cancelOrderExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				orderJobService.cancelOutTimeOrders();
			}
		});
	}

	/*
	 * 更新搜索服务数据
	 */
	public synchronized void doUpdateSearchDataTask() {
		updateSearchDataExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				searchJobService.updateSearchData();
			}
		});
	}

	public synchronized void doUpdateApiClientTask() {
		updateApiClientExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				apiClientJobService.updateCounts();
			}
		});
	}

	public synchronized void doStatisticsTask() {
		statisticsExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				statisticsJobService.executeStatistics();
			}
		});
	}
}
